import static java.lang.Thread.sleep;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
 * Created with IntelliJ IDEA.
 * User: Tylaar
 * Date: 9/9/12
 * Time: 8:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class LittleNode implements Runnable {

    private long node_id;
    private final int portLowBound = 20000;
    private final int portHighBound = 25000;

    private LitteNodeStubInterface litteNodeStubInterface;

    public LittleNode(long node_id) {
        this.node_id = node_id;
    }

    public long getNode_id() {
        return node_id;
    }

    public void setNode_id(long node_id) {
        this.node_id = node_id;
    }

    public String getStatus(){
        return "status: " + "\nnode_id: " + node_id;
    }

    private int getRandomPort(){
        int randomPort = portLowBound + (int)(Math.random() * ((portHighBound - portLowBound) + 1));
        System.out.println("A random port is generated: " + randomPort);
        return randomPort;
    }

    public void rmiRegist()throws Exception{
        int randomPort = getRandomPort();
        LocateRegistry.createRegistry(randomPort);
        this.litteNodeStubInterface = new LittleNodeStubImpl(node_id);
        Naming.bind("LittleNode"+node_id, this.litteNodeStubInterface);
        System.out.println("LittleNode is registered in the Naming space");
    }

    private void randomlySayHi(){
        try {
            long target_id = (long)(Math.random() * 5);
            System.out.println("try to connect target: " + target_id);
            LitteNodeStubInterface target = (LitteNodeStubInterface)Naming.lookup("LittleNode" + target_id);
            target.receiveHi(this.node_id);
        } catch (NotBoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void run(){
        try {
            rmiRegist();
            while (true) {
                int randonTimePeriod = (int)(Math.random() * 2999);
                sleep(randonTimePeriod);
                //String helloWord = "Hi ,this is thread : " + node_id;
                randomlySayHi();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
}
